package com.zzyl.redis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedisTest {
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test() {
        System.out.println(redisTemplate);
    }

    @Test
    //普通set方法
    public void testString() {
        redisTemplate.opsForValue().set("name", "莱莱");
        System.out.println(redisTemplate.opsForValue().get("name"));

        //设置带有过期时间的key
        redisTemplate.opsForValue().set("token", "macaron", 20, TimeUnit.SECONDS);
        System.out.println(redisTemplate.opsForValue().get("token"));

        //setnx 当指定的键key不存在时，会将key的值设置为value，返回true，否则返回false
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "09876", 5, TimeUnit.MINUTES));
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "12345", 5, TimeUnit.SECONDS));
    }


    //操作hash类型
    @Test
    public void testHash() {
        //hash请求 大key 小key value
        redisTemplate.opsForHash().put("user", "name", "莱莱");
        redisTemplate.opsForHash().put("user", "age", "17");

        //大hash的key
        Object o = redisTemplate.opsForHash().get("user", "name");
        Object ob = (redisTemplate.opsForHash().get("user", "age"));
        System.out.println(o+""+ob);

        //取小hash的所有key
        Set<Object> keySet =redisTemplate.opsForHash().keys("user");
        System.out.println(keySet);

        //取小hash的所有value
        List<Object> valueSet = redisTemplate.opsForHash().values("user");
        System.out.println(valueSet);

        //删除一个值
        redisTemplate.opsForHash().delete("user","age");

        //删除大key ，整个hash都会没有
        redisTemplate.delete("user");

    }

    //操作列表数据类型
    @Test
    public void testList(){
        //插入值
        redisTemplate.opsForList().leftPushAll("list","默默","小雾","莱莱");

        //在列表右边插入一个数据
        redisTemplate.opsForList().rightPush("list","伊伊");

        //获取列表中的数据
        List<String> list = redisTemplate.opsForList().range("list", 0, -1);
        System.out.println(list);

        //从右边弹出一个，并获取值，弹出后在列表中删除
        redisTemplate.opsForList().rightPop("list");
        System.out.println(redisTemplate.opsForList().range("list", 0, -1));

        //获取列表长度
        System.out.println(redisTemplate.opsForList().size("list"));
    }


    //操作集合类型的数据
@Test
    public void testSet(){
        //添加数据
    redisTemplate.opsForSet().add("earphones","咏叹调2","天鹤座Ultra","花再");
    redisTemplate.opsForSet().add("earphones2","赛睿nove5","花再","水月雨");

    //获取集合中的所有成员
    Set<String> earphones = redisTemplate.opsForSet().members("earphones");
    Set<String> earphones2 = redisTemplate.opsForSet().members("earphones2");
    System.out.println(earphones);
    System.out.println(earphones2);

    //获取集合大小
    System.out.println(redisTemplate.opsForSet().size("earphones"));
    System.out.println(redisTemplate.opsForSet().size("earphones2"));

    //交集
    System.out.println(redisTemplate.opsForSet().intersect("earphones","earphones2"));

    //并集
    System.out.println(redisTemplate.opsForSet().union("earphones","earphones2"));

}

//操作有序集合类型的数据
    @Test
    public void testZSet() {
        //添加数据
        redisTemplate.opsForZSet().add("age", "莱莱", 17);
        redisTemplate.opsForZSet().add("age", "小雾", 18);
        redisTemplate.opsForZSet().add("age", "舒茉", 15);
        redisTemplate.opsForZSet().add("age", "默默", 18);

        //查询所有
        System.out.println(redisTemplate.opsForZSet().range("age", 0, -1));

        //自增
        redisTemplate.opsForZSet().incrementScore("age", "舒茉", 1);

        //再查询所有
        System.out.println(redisTemplate.opsForZSet().range("age", 0, -1));

        //删除舒茉小学生
        redisTemplate.opsForZSet().remove("age", "舒茉");

    }
        //通用命令操作
        @Test
        public void testCommon() {
            // 获取所有key
            Set<String> keys = redisTemplate.keys("*");
            System.out.println(keys);

            // 判断key是否存在
            Boolean isName = redisTemplate.hasKey("name");
            System.out.println(isName);

            // 获取key的类型
            DataType type = redisTemplate.type("list");
            System.out.println(type.name());

            // 删除key
            redisTemplate.delete("list");

    }

}